package cn.yshujia.service.impl;

import cn.yshujia.domain.dto.PageDTO;
import cn.yshujia.domain.entity.Develop;
import cn.yshujia.domain.vo.PageVO;
import cn.yshujia.mapper.DevelopMapper;
import cn.yshujia.repository.DevelopRepository;
import cn.yshujia.utils.TimeUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.apache.ibatis.executor.BatchResult;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author YShuJia
 * @since 2024/4/23
 */
@Service
public class DevelopServiceImpl extends ServiceImpl<DevelopMapper, Develop> implements IService<Develop> {
	
	@Resource (name = "Rest")
	private RestTemplate restTemplate;
	
	@Resource
	private DevelopMapper mapper;
	
	@Resource
	private DevelopRepository repository;
	
	final String urlStr = "https://gitee.com/api/v5/repos/yshujia-utopia/utopia/commits";
	
	final String branchStr = "?sha=";
	
	final String timeStr = "&since=";
	
	final String pageStr = "&page=1&per_page=100";
	
	
	public PageVO<Develop> pageByBranch(PageDTO dto) {
		return repository.pageByBranch(dto);
	}
	
	public Boolean insert(String branch) {
		
		Develop developOld = mapper.selectOne(new LambdaQueryWrapper<Develop>()
				.eq(Develop::getBranch, branch)
				.orderByDesc(Develop::getId).last("limit 1"));
		
		if (null == developOld) {
			developOld = new Develop();
			developOld.setTime(TimeUtils.getDateTime("2024-04-23"));
		}
		
		String url = urlStr + branchStr + branch + timeStr + developOld.getTime() + pageStr;
		
		ResponseEntity<DevelopEntity[]> responseEntity = restTemplate.getForEntity(url, DevelopEntity[].class);
		DevelopEntity[] developEntity = responseEntity.getBody();
		
		if (!ObjectUtils.isEmpty(developEntity)) {
			List<Develop> list = new ArrayList<>();
			for (int i = developEntity.length - 1; i >= 0; i--) {
				Develop develop = new Develop();
				develop.setSha(developEntity[i].sha);
				develop.setBranch(branch);
				develop.setAuthor(developEntity[i].commit.committer.name);
				develop.setEmail(developEntity[i].commit.committer.email);
				develop.setTime(developEntity[i].commit.committer.date);
				develop.setHome(developEntity[i].committer.html_url);
				develop.setAvatar(developEntity[i].committer.avatar_url);
				develop.setMessage(developEntity[i].commit.message);
				develop.setUrl(developEntity[i].html_url);
				if (null != developEntity[i].parents) {
					List<String> shaList = developEntity[i].parents.stream().map(Parents::getSha).toList();
					develop.setParentSha(String.join(",", shaList));
				}
				develop.setStatus(true);
				list.add(develop);
			}
			List<BatchResult> n = mapper.insert(list);
			repository.del(branch);
			return !n.isEmpty();
		}
		return false;
	}
	
	static class DevelopEntity {
		
		public String sha;
		// 提交地址
		public String html_url;
		
		public Commit commit;
		
		public List<Parents> parents;
		
		public Committer committer;
		
		
		static class Committer {
			public String html_url;
			public String avatar_url;
		}
	}
	
	static class Commit {
		
		public String message;
		
		public Committer committer;
	}
	
	static class Committer {
		public String name;
		
		public String email;
		
		public Date date;
	}
	
	static class Parents {
		
		public String sha;
		
		public String getSha() {
			return this.sha;
		}
	}
}
